#include "LinkTree.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define true 1
#define false 0
struct student
{
    int id;
    char *name;
    int score;
};

struct student *CreateStudent(int id, const char *name, int score)
{
    struct student *stu = (struct student *)malloc(sizeof(struct student));//使用 malloc 函数动态分配内存，
    //分配了一个 struct student 大小的内存块，并将其指针赋给 stu。
    if (stu == NULL)
    {
        return NULL;
    }
    stu->id = id;
    stu->name = (char *)name;
    stu->score = score;
}

void FreeStudent(struct student *stu)
{
    if (stu == NULL)
        return;

    free(stu);
}

void Print(ElementType data)
{
    if (data.type == structPoint)//检查传入的 data 的类型是否为 structPoint，即结构体指针类型。
    {
        struct student *stu = (struct student *)data.value;//将传入的数据的值赋给 stu，并将其强制转换为结构体指针类型。
        if (stu != NULL)
        {
            printf("id : %d ,name : %s,score : %d\n", stu->id, stu->name, stu->score);
        }
    }
    else if (data.type == charPoint)//字符指针类型。
    {
        char *temp = (char *)data.value;//将传入的数据的值赋给 temp，并将其强制转换为字符指针类型。
        printf("%s\n", temp);
    }
}

int IsEqual(ElementType data1, ElementType data2)//用于比较两个 ElementType 类型的数据是否相等
{
    if (data1.type == charPoint && data2.type == charPoint)//检查两个数据的类型是否都为 charPoint，即字符指针类型。
    {
        struct student *stu = (struct student *)data1.value;
        struct student *stu2 = (struct student *)data2.value;
        if (stu->id == stu2->id)
        {
            return true;
        }
        return false;
    }
    else if (data1.type == charPoint && data2.type == charPoint)//如果两个数据的类型都为 charPoint
    {
        char *temp = (char *)data1.value;//将 data1 的值强制转换为字符指针类型，并赋给 temp。
        char *temp2 = (char *)data2.value;//将 data2 的值强制转换为字符指针类型，并赋给 temp2。
        if (strcmp(temp, temp2) == 0)
        {
            return true;
        }
        return false;
    }
}

int main()
{
    LTree tree;
    InitLTree(&tree);

    struct UniversalType TypeValue;
    TypeValue.value = "243class";
    TypeValue.type = charPoint;
    LTNode *node1 = CreateTreeNode(TypeValue);

    TypeValue.value = CreateStudent(1, "wjk", 100);
    TypeValue.type = structPoint;
    LTNode *node2 = CreateTreeNode(TypeValue);

    TypeValue.value = CreateStudent(2, "yxqx", 90);
    TypeValue.type = structPoint;
    LTNode *node3 = CreateTreeNode(TypeValue);

    // LTNode *node2 = CreateTreeNode(CreateStudent(1,"wjk",100));
    // LTNode *node3 = CreateTreeNode(CreateStudent(2,"yxqx",90));

    ConnectBranch(node1, node2);
    ConnectBranch(node1, node3);

    // LTNode *node5 = CreateTreeNode("247class");
    // LTNode *node6 = CreateTreeNode("wyf");
    // LTNode *node7 = CreateTreeNode("zyx");
    // LTNode *node8 = CreateTreeNode("hzt");
    // ConnectBranch(node5,node6);
    // ConnectBranch(node5,node7);
    // ConnectBranch(node5,node8);

    ConnectBranch(tree.root, node1);
    // ConnectBranch(tree.root,node5);

    Travel(&tree, Print);
    // printf("height : %d\n",GetTreeHeight(&tree));

    // LTNode *classNode = FindTreeNode(&tree,"243class");
    // if (classNode != NULL)
    // {
    //     LTNode *newNode = CreateTreeNode("wh");
    //     ConnectBranch(classNode,newNode);
    // }
    // Travel(&tree);
    TypeValue.type = charPoint;
    // TypeValue.type = structPoint;
    TypeValue.value = "243class";
    // TypeValue.value = CreateStudent(1,"",0);
    LTNode *findnode = FindTreeNode(&tree, TypeValue, IsEqual);
    // findnode = FindTreeNode(&tree,TypeValue,IsEqual);
    if (findnode != NULL)
    {
        char *className = "247class";
        findnode->data.value = className;
        // struct student *stu = (struct student *)findnode->data.value;
        // stu->score = 60;
    }
    // free(TypeValue,value);
    Travel(&tree, Print);
    return 0;
}